<?php

namespace addons\shopro\controller;

use think\Db;

class UserWalletApply extends Base
{

    protected $noNeedLogin = ['rule'];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $this->success('提现记录', \addons\shopro\model\UserWalletApply::getList());
    }

    // 申请提现
    public function apply()
    {
        $type = $this->request->post('type');
        if(empty($type)){
            $type = 'bank';
        }
        if($type == 'bank'){
            $user = $this->auth->getUserinfo();
            $row = Db::table('fa_shopro_user_bank')->where('user_id',$user['id'])->find();
            if(empty($row)){
                $this->error('请先修改绑定银行卡');
            }
        }
        if($type == 'wechat'){
            if(empty(input('openid'))){
                 $this->error('未微信授权');
            }else{
              /*  $user = $this->auth->getUser();
                if(empty($user['openid'])){
                    $user->openid = input('openid');
                    $user->save();
                }*/
                $user = $this->auth->getUserinfo();
                $oauth = Db::table('fa_shopro_user_oauth')->where('user_id',$user['id'])->find();
                if(empty($oauth)){
                    $oauth = [
                        'user_id' => $user['id'],
                        'provider' => 'Wechat',
                        'platform' => 'wxMiniProgram',
                        'openid' => input('openid'),
                        'nickname' => $user['nickname'],
                        'createtime' => time(),
                        'updatetime' => time(),
                    ];
                    Db::table('fa_shopro_user_oauth')->insert($oauth);
                }
            }
        }

        $money = $this->request->post('money');
        $apply = \think\Db::transaction(function () use ($type, $money) {
            try {
                return \addons\shopro\model\UserWalletApply::apply($type, $money);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        });
        if($apply) {
            $this->success('申请成功');
        }
        $this->error('申请失败');
    }


    public function rule()
    {
        // 提现规则
        $config = \addons\shopro\model\UserWalletApply::getWithdrawConfig();
        $min = round(floatval($config['min']), 2);
        $max = round(floatval($config['max']), 2);
        $service_fee = floatval($config['service_fee']) * 100;
        $service_fee = round($service_fee, 1);      // 1 位小数
        $perday_amount = isset($config['perday_amount']) ? round(floatval($config['perday_amount']), 2) : 0;
        $perday_num = isset($config['perday_num']) ? round(floatval($config['perday_num']), 2) : 0;

        $rule = [
            'min' => $min,
            'max' => $max,
            'service_fee' => $service_fee,
            'perday_amount' => $perday_amount,
            'perday_num' => $perday_num,
            'methods' => $config['methods'] ?? []
        ];

        $this->success('提现规则', $rule);
    }
}
